package com.fly.control;

import java.sql.ResultSet;

import com.fly.conn.DBConnect;
import com.fly.model.Supplier;

public class SupplierControl {
	public static boolean insertSupplier(Supplier s){
		DBConnect dbc = new DBConnect();
		String sql = "insert into paramsupplier(companyName, linkName, telephone, address, bankName, bankId, remark) values(?, ?, ?, ?, ?, ?, ?)";
		dbc.prepareStatement(sql);
		try{
			int i = 1;
			dbc.setString(i++, s.getCompanyName());
			dbc.setString(i++, s.getLinkName());
			dbc.setString(i++, s.getTelephone());
			dbc.setString(i++, s.getAddress());
			dbc.setString(i++, s.getBankName());
			dbc.setString(i++, s.getBankId());
			dbc.setString(i++, s.getRemark());
			if(dbc.executeUpdate())
			return true;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("insertSupplier ERROR");
		} finally{
			dbc.close();
		}
		return false;
	}
	public static boolean deleteSuplier(int id){
		DBConnect dbc = new DBConnect();
		String sql = "delete * from paramsupplier where id =" + id;
		dbc.prepareStatement(sql);
		try{
			if(dbc.executeUpdate()){
				return true;
			}
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("deleteSuplier ERROR");
		} finally{
			dbc.close();
		}
		return false;
	}
	public static boolean updateSupplier(Supplier s, int id){
		DBConnect dbc = new DBConnect();
		String sql = "update paramsupplier set companyName=?, linkName=?, telephone=?, address=?, bankName=?, bankId=?, remark=? where id =" + id;
		dbc.prepareStatement(sql);
		try{
			int i = 1;
			dbc.setString(i++, s.getCompanyName());
			dbc.setString(i++, s.getLinkName());
			dbc.setString(i++, s.getTelephone());
			dbc.setString(i++, s.getAddress());
			dbc.setString(i++, s.getBankName());
			dbc.setString(i++, s.getBankId());
			dbc.setString(i++, s.getRemark());
			if(dbc.executeUpdate())
				return true;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("updateSupplier ERROR");
		} finally{
			dbc.close();
		}
		return false;
	}
	public static Supplier getSupplier(int id){
		DBConnect dbc = new DBConnect();
		ResultSet rs = null;
		String sql = "select * from paramsupplier where id =" + id;
		dbc.prepareStatement(sql);
		Supplier s = new Supplier();
		try{
			rs = dbc.executeQuery();
			while(rs.next()){
				s.setAddress(rs.getString("address"));
				s.setBankId(rs.getString("bankId"));
				s.setBankName(rs.getString("bankName"));
				s.setCompanyName(rs.getString("companyName"));
				s.setLinkName(rs.getString("linkName"));
				s.setRemark(rs.getString("remark"));
				s.setTelephone(rs.getString("telephone"));
			}
			return s;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("updateSupplier ERROR");
		} finally{
			dbc.close();
		}
		return null;
	}
}
